class RepliesController < ApplicationController
  before_filter :find_guest_name, :only => :create
  
  def new 
    @reply = Reply.new
    @reply.name = session[:name] unless session[:name].nil?
  end
  
  def index
    find_current_movie_and_replies :page => params[:page], :movie_id => params[:movie_id]
  end
  
  
  
  def create 
    @reply = Reply.new(params[:reply])
    @reply.movie_id = params[:movie_id]
    find_current_movie_and_replies :movie_id => params[:movie_id]
    puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    puts @reply.movie_id
    if @reply.save
      flash[:notice] = "Ok"
      render :action => :index
    else
      flash[:notice] = "NO"
      render :action => :index
    end
  end
  
  
  
  def destroy
    @reply = Reply.find(params[:id])
    @reply.destroy
  end
  
  private
  
  def find_guest_name
    params[:reply][:name] = 'guest' if params[:reply][:name].blank? 
  end
  
  def find_current_movie_and_replies(options = {})
    page = options[:page].nil? ? 1 : options[:page]
    @movie = Movie.find(options[:movie_id])
    @replies = Reply.paginate_by_movie_id options[:movie_id], :page => page, :order => "created_at DESC"
  end
  
end
